package yxm.zyf.love.utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class VolatileTest {
    
    private static AtomicInteger a= new AtomicInteger(1); 
    
    private static int nThreads = Runtime.getRuntime().availableProcessors() * 2 + 1;

    private static ExecutorService executors = Executors.newFixedThreadPool(nThreads, new ThreadFactory() {

        private final String threadNamePrefix = "si_query_task_";

        private final AtomicInteger count = new AtomicInteger(1);

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(Thread.currentThread().getThreadGroup(), r, threadNamePrefix + count.getAndIncrement());
            t.setDaemon(true);
            return t;
        }
    });

    public static void main(String[] args) {
        for (int i = 0; i < 90; i++) {
            Task t1 =new Task();
            executors.execute(t1);
        }
        System.out.println(String.format("最终结果:%s", a));
    }
    
    static class Task implements Runnable{

        @Override
        public void run() {
            
            a.getAndAdd(1);
            System.out.println(a);
        }
        
    }
}
